package com.coder.shop.admin.controller.manage;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.coder.common.base.BaseController;
import com.coder.shop.common.constant.ShopResult;
import com.coder.shop.common.constant.ShopResultConstant;
import com.coder.shop.dao.model.ShopGoodsType;
import com.coder.shop.dao.model.ShopGoodsTypeExample;
import com.coder.shop.dao.model.ShopGoodsTypeSpec;
import com.coder.shop.dao.model.ShopSpec;
import com.coder.shop.dao.model.ShopSpecExample;
import com.coder.shop.rpc.api.ShopGoodsTypeService;
import com.coder.shop.rpc.api.ShopSpecService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 分类controller
 * @Author liuzhimin
 * @Date Created by liuzhimin on 2018/4/24.
 * @Description
 */
@Controller
@RequestMapping("/manage/goodsType")
@Api(value = "分类控制器", description = "分类管理")
public class ShopGoodsTypeController extends BaseController {

    private static final Logger LOGGER = LoggerFactory.getLogger(ShopGoodsTypeController.class);

    @Autowired
	private ShopGoodsTypeService shopGoodsTypeService;
    
    @Autowired
    private ShopSpecService shopSpecService;
	
	@ApiOperation(value = "分类首页")
	@RequiresPermissions("shop:goodsType:read")
	@RequestMapping(value = "/index", method = RequestMethod.GET)
	public String index() {
		return "/manage/goodsType/index.jsp";
	}

	@ApiOperation(value = "分类列表")
	@RequiresPermissions("shop:goodsType:read")
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	@ResponseBody
	public Object list(
			@RequestParam(required = false, defaultValue = "0", value = "offset") int offset,
			@RequestParam(required = false, defaultValue = "10", value = "limit") int limit,
			@RequestParam(required = false, value = "sort") String sort,
			@RequestParam(required = false, value = "order") String order) {
		ShopGoodsTypeExample shopGoodsTypeExample = new ShopGoodsTypeExample();
		if (!StringUtils.isBlank(sort) && !StringUtils.isBlank(order)) {
			shopGoodsTypeExample.setOrderByClause(sort + " " + order);
		}
		List<ShopGoodsType> rows = shopGoodsTypeService.selectByExampleForOffsetPage(shopGoodsTypeExample, offset, limit);
		long total = shopGoodsTypeService.countByExample(shopGoodsTypeExample);
		Map<String, Object> result = new HashMap<>(2);
		result.put("rows", rows);
		result.put("total", total);
		return result;
	}
	
	@ApiOperation(value = "分类新增")
	@RequiresPermissions("shop:goodsType:create")
	@RequestMapping(value = "/create", method = RequestMethod.GET)
	public String create(ModelMap modelMap) {
		
		List<ShopSpec> shopSpecList = shopSpecService.selectByExample(new ShopSpecExample());
		modelMap.put("shopSpecList", shopSpecList);
		
		return "/manage/goodsType/create.jsp";
	}

	@ApiOperation(value = "分类新增")
	@RequiresPermissions("shop:goodsType:create")
	@RequestMapping(value = "/create", method = RequestMethod.POST)
	@ResponseBody
	public Object create(ShopGoodsType shopGoodsType,Integer[] specIds) {
		int count = shopGoodsTypeService.addGoodsType(shopGoodsType,specIds);
		return new ShopResult(ShopResultConstant.SUCCESS, count);
	}
	
		@ApiOperation(value = "删除分类")
	@RequiresPermissions("shop:goodsType:delete")
	@RequestMapping(value = "/delete/{ids}",method = RequestMethod.GET)
	@ResponseBody
	public Object delete(@PathVariable("ids") String ids) {
		int count = shopGoodsTypeService.deleteByPrimaryKeys(ids);
		return new ShopResult(ShopResultConstant.SUCCESS, count);
	}

	@ApiOperation(value = "修改分类")
	@RequiresPermissions("shop:goodsType:update")
	@RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
	public String update(@PathVariable("id") int id, ModelMap modelMap) {
		ShopGoodsType shopGoodsType = shopGoodsTypeService.selectByPrimaryKey(id);
		modelMap.put("shopGoodsType", shopGoodsType);
		
		List<ShopSpec> shopSpecList = shopSpecService.selectByExample(new ShopSpecExample());
		modelMap.put("shopSpecList", shopSpecList);
		
		
		List<ShopSpec> specIds = shopGoodsType.getShopSpecList();
		
		List<Integer> checkedSpecIds = new ArrayList<Integer>();
		for(ShopSpec shopSpec : shopSpecList) {
			if(specIds.contains(shopSpec)) {
				checkedSpecIds.add(1);
			}else {
				checkedSpecIds.add(0);
			}
		}
		modelMap.put("checkedSpecIds", checkedSpecIds);
		
		return "/manage/goodsType/update.jsp";
	}

	@ApiOperation(value = "修改分类")
	@RequiresPermissions("shop:goodsType:update")
	@RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
	@ResponseBody
	public Object update(@PathVariable("id") int id, ShopGoodsType shopGoodsType,Integer[] specIds) {
		shopGoodsType.setId(id);
		int count = shopGoodsTypeService.updateGoodsType(shopGoodsType, specIds);
		return new ShopResult(ShopResultConstant.SUCCESS, count);
	}

}